/*
 * PageImpl_.h
 *
 *  Created on: Aug 1, 2011
 *      Author: yong
 */

#ifndef PAGEIMPL__H_
#define PAGEIMPL__H_
#include<cstddef>
#include<string>
#include "KeyValue.h"
#include "Typedef.h"

using namespace std;
namespace distrim
{
/**
 * A recording structure records every page's state, every input data record
 * should belong to one of the instances.
 */

template<typename K, typename V>
class Page<K, V>
{

public:
	Page(const size_t pageID, void *ptrPageBeg = NULL, const string &fileName = "") :
		m_fileName(fileName), m_pageID(pageID), m_ptrPageBeg(ptrPageBeg),
				m_isWrittenToDisk(false), m_storageState(StorageState::Empty)
	{
	}
	virtual ~Page()
	{
	}

protected:

	/**
	 * Number of page, read only.
	 */
	size_t m_pageID;
	/**
	 * The beginning pointer of memory page allocated by memory page master,
	 * 	if it is not in memory, then should be NULL.
	 */
	void *m_ptrPageBeg;
	/**
	 *
	 */
	string m_fileName;
	/**
	 * Whether this page has ever been written to disk or not.
	 */
	bool m_isWrittenToDisk;
	/**
	 * Whether this page is in memory so no need to refetch from disk.
	 */
	StorageState::StorageStateEnum m_storageState;

public:

	/**
	 * To be overridden by inheritted classes.
	 */
	virtual void ReadFromDisk() = 0;
	/**
	 * To be overridden by inheritted classes.
	 */
	virtual void WriteToDisk() = 0;
	/**
	 * Set page begin.
	 */
	inline void SetPageBegin(void *ptrBeg)
	{
		m_ptrPageBeg = ptrBeg;
	}
	/**
	 * Set page id.
	 */
	inline void SetPageID(size_t pageID)
	{
		m_pageID = pageID;
	}
	/**
	 * Get page id.
	 */
	inline const size_t GetPageID() const
	{
		return m_pageID;
	}
	/**
	 * Set page storage state: not allocated, allocated or filled.
	 */
	inline void SetStorageState(StorageState::StorageStateEnum storageState)
	{
		m_storageState = storageState;
	}
	/**
	 * Get page storage state: not allocated, allocated or filled.
	 */
	inline StorageState::StorageStateEnum &GetStorageState() const
	{
		return m_storageState;
	}
	/**
	 * Set whether this page has ever been written to disk or not.
	 */
	inline void SetIsWrittenToDisk(bool isWrittenToDisk)
	{
		m_isWrittenToDisk = isWrittenToDisk;
	}
	/**
	 * Get whether this page has ever been written to disk or not.
	 */
	inline const bool IsWrittenToDisk() const
	{
		return m_isWrittenToDisk;
	}

};

}

#endif /* PAGEIMPL__H_ */
